#include "mainDriver.h"

mainDriver::mainDriver() 
{

}

void* mainDriver::login(string type)
{
    dbMemberAccount('s', allLogVec);
    
    vector<MemberAccount*>::iterator p;
    string uname,pwd;
    bool userOK = false,passOK = false,correctType = false;
    int count = 0;
    
    while(!userOK)
    {
        p = allLogVec.begin();
        count = 0;
        
        cout << "Enter " << type << " ID : ";
        getline(cin,uname);
        
        if(isalpha(uname[0]))
            uname[0] = toupper(uname[0]);
        
        while(p != allLogVec.end() && !userOK)
        {  
            if(uname == (*p)->getMemberID())
            {
                userOK = true;

                if((*p)->getType() == type)
                {
                    correctType = true;
                }
            }
            else
            {
               ++count;
               ++p;
            }
        }
        
        if(!userOK)
        {
            cout << "Error: " << type << " ID not found. Re-";
        }
        else if(!correctType)
        {
            userOK = false;
            cout << "Error: This Id is not a " << type << ". Re-";
        }
    }
    
    if(userOK)
    {
        while(!passOK)
        {
            cout << "Enter Password : ";
            getline(cin,pwd);
            
            if(pwd == (*(allLogVec[count])).getMemberPassword())
            {
                passOK = true;
                allLogVec[count]->setAuthenticate(false);
            }
            
            if(!passOK)
            {
                cout << "Error: Incorrect Password. Re-enter Password: ";
            }
        }
    }
    
    if(!(*allLogVec[count]).getAuthenticate())
    {
        cout << endl << "User Not Authenticate! Please Authenticate your details below: " << endl;
        
        bool correct = false;
        
        while(!correct)
        {
            bool optionC = false;
            
            while(!optionC)
            {
                cout << endl;
                
                if((*allLogVec[count]).getType() == "Member")
                {
                    cout << "Member Id     : " << (*allLogVec[count]).getMemberID() << endl;
                    cout << "Name          : " << (*allLogVec[count]).getMemberName() << endl;
                }
                else if ((*allLogVec[count]).getType() == "Admin")
                {
                    cout << "Admin Id      : " << (*allLogVec[count]).getMemberID() << endl;
                    cout << "Name          : " << (*allLogVec[count]).getMemberName() << endl;
                }
            
                cout << "Is the above information correct(y/n) : ";
            
                string ch;
            
                getline(cin,ch);
            
                ch = tolower(ch[0]);
                
                if(ch.size() > 1)
                    cout << "Error: Detected an sentence! Re-enter option!" << endl << endl;
                else if (!((ch != "y") || (ch != "n")))
                    cout << "Error: Detected an Invalid option! Re-enter option!" << endl << endl;
                else
                {
                    if((ch == "y"))
                    {
                        optionC = true;
                        correct = true;
                        (*allLogVec[count]).setAuthenticate(true);
                    }
                    else if(ch == "n")
                    {
												string newMemName;
												bool correct = false;
												cout << endl;

												cout << "Old Member Name : " << (*allLogVec[count]).getMemberName() << endl;
												cout << "New Member Name : ";

												getline(cin,newMemName);

												(*allLogVec[count]).setMemberName(newMemName);
                    }
                }
            }
         }   
    }
   
   void* userAcc =  allLogVec[count];
   return userAcc;
}

void mainDriver::logout()
{
	dbMemberAccount('u', allLogVec);
}
